package Model;

import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;


public class Restaurant {

        public final long restaurantID;
        public final String restaurantName;
        public final String phoneNumber;
        public final String address;
        public final Date openHour;
        public final Date closeHour;      
        public final String logoAddress;
        public final String photos;
    
        
        public final ArrayList<URL> listOfPicture () {
                ReadFromURL rfu = new ReadFromURL();
                try {
                	String content = rfu.getContentFromURL(new URL(photos));
					JSONArray arr = new JSONArray(content);
					ArrayList<URL> result = new ArrayList<URL>();
					for (int i = 0; i < arr.length(); i++) {
						result.add(new URL(arr.getString(i)));
					}
					return result;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                return null;           
        }
            
        public Restaurant (long rid) {
                restaurantID = rid;
                String _photos = null, _restaurantName = null, _phoneNumber = null, _address = null, _openHour = null, _closeHour = null, _logoAddr = null; 
                try {
                        URL url = new URL("http://happywaiman.dlinkddns.com/Reserve/Company/Info.php?id="+rid);
                        ReadFromURL reader = new ReadFromURL();
                        String content = reader.getContentFromURL(url);
                        JSONObject obj = new JSONObject(content);
                        @SuppressWarnings("unchecked")
                        Iterator<String> it = obj.keys();
                        while (it.hasNext()) {
                                String key = it.next();
                                if ("CompanyName".equalsIgnoreCase(key)) {
                                        _restaurantName = obj.getString(key);
                                } else if ("phoneNumber".equalsIgnoreCase(key)) {
                                        _phoneNumber = obj.getString(key);
                                } else if ("StoreAddr".equalsIgnoreCase(key)) {
                                        _address = obj.getString(key);
                                } else if ("OpenHour".equalsIgnoreCase(key)) {
                                        _openHour = obj.getString(key);
                                } else if ("CloseHour".equalsIgnoreCase(key)) {
                                        _closeHour = obj.getString(key);
                                } else if ("LogoAddress".equalsIgnoreCase(key)) {
                                        _logoAddr = obj.getString(key);
                                } else if ("ListofPicture".equalsIgnoreCase(key)) {
                                		_photos = obj.getString(key);
                                }
                        }
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                restaurantName = _restaurantName;
                phoneNumber = _phoneNumber;
                address = _address;
                photos = _photos;
                DateFormat formatter = DateFormat.getTimeInstance();
                Date tmp = null;
                try {
                        tmp = formatter.parse(_openHour);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                openHour = tmp;
                tmp = null;
                try {
                        tmp = formatter.parse(_closeHour);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                closeHour = tmp;
                logoAddress = _logoAddr;
                
        }
        public Boolean currentAvailable(int numberOfSeat) {
        	String addressFormat = "http://happywaiman.dlinkddns.com/Reserve/Company/table.php?rid="+restaurantID+"&seat=";
        	String address = addressFormat+numberOfSeat;
        	ReadFromURL rfu = new ReadFromURL();
        	String content;
			try {
				content = rfu.getContentFromURL(new URL(address));
	        	Integer numberOfTable = Integer.parseInt(""+content.charAt(0));
	        	return numberOfTable.equals(1);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
        }

}